from langchain.document_loaders import AcreomLoader, AirbyteJSONLoader, AirtableLoader, MaxComputeLoader, \
    BiliBiliLoader, ChatGPTLoader, CollegeConfidentialLoader, UnstructuredEmailLoader, OutlookMessageLoader, GitLoader, \
    UnstructuredImageLoader, ImageCaptionLoader, UnstructuredXMLLoader
from langchain.document_loaders.blockchain import BlockchainType, BlockchainDocumentLoader
from langchain.indexes import VectorstoreIndexCreator


def loader_with_acreom():
    loader = AcreomLoader("files/", collect_metadata=False)
    print(loader.load())


def loader_with_air_byte_json():
    loader = AirbyteJSONLoader("files/fake_conversations.json")
    data = loader.load()
    print(data[0].page_content[:1])


# 加载表格 pip install pyairtable
def loader_with_air_table():
    loader = AirtableLoader("api_key", "table_id", "base_id")
    loader.load()


def loader_with_alibaba_max_compute():
    # pip install pyodps
    base_query = """
    SELECT *
    FROM (
        SELECT 1 AS id, 'content1' AS content, 'meta_info1' AS meta_info
        UNION ALL
        SELECT 2 AS id, 'content2' AS content, 'meta_info2' AS meta_info
        UNION ALL
        SELECT 3 AS id, 'content3' AS content, 'meta_info3' AS meta_info
    ) mydata;
    """
    endpoint = "<ENDPOINT>"
    project = "<PROJECT>"
    ACCESS_ID = "<ACCESS ID>"
    SECRET_ACCESS_KEY = "<SECRET ACCESS KEY>"
    loader = MaxComputeLoader.from_params(
        base_query,
        endpoint,
        project,
        access_id=ACCESS_ID,
        secret_access_key=SECRET_ACCESS_KEY,
    )
    loader.load()


def loader_with_block_chain():
    alchemyApiKey = "jiSWkgxwYxoe5Tm37Lza8nqavzZXConq"
    contractAddress = "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"  # Bored Ape Yacht Club 合约地址
    blockchainType = BlockchainType.ETH_MAINNET  # 默认值，可选参数
    blockchainLoader = BlockchainDocumentLoader(
        contract_address=contractAddress, api_key=alchemyApiKey
    )
    blockchain_type_ntfs = blockchainLoader.load()
    print(blockchain_type_ntfs)
    contractAddress = (
        "0x448676ffCd0aDf2D85C1f0565e8dde6924A9A7D9"  # Polygon主网合约地址
    )
    blockchainType = BlockchainType.POLYGON_MAINNET
    blockchainLoader = BlockchainDocumentLoader(
        contract_address=contractAddress,
        blockchainType=blockchainType,
        api_key=alchemyApiKey,
    )
    polygon_ntfs = blockchainLoader.load()
    print(polygon_ntfs)


def loader_with_bilibili():
    loader = BiliBiliLoader(["https://www.bilibili.com/video/BV1xt411o7Xu/"])
    print(loader.load())


def loader_with_chatgpt():
    loader = ChatGPTLoader(log_file="files/fake_conversations.json", num_logs=1)
    print(loader.load())


def loader_with_college_confidential():
    loader = CollegeConfidentialLoader(
        "https://www.collegeconfidential.com/colleges/brown-university/"
    )
    print(loader.load())


def load_with_email():
    # loader = UnstructuredEmailLoader("files/fake-email.eml")
    # print(loader.load())
    loader = OutlookMessageLoader("files/fake-email.msg")
    print(loader.load()[0])


def loader_with_git():
    from git import Repo

    repo = Repo.clone_from(
        "https://github.com/hwchase17/langchain", to_path="./example_data/test_repo1"
    )
    branch = repo.head.reference
    loader = GitLoader(repo_path="./example_data/test_repo1/", branch=branch)
    print(loader.load())


def git_clone():
    # file_filter 过滤要加载的文件
    loader = GitLoader(
        clone_url="https://github.com/hwchase17/langchain",
        repo_path="./example_data/test_repo2/",
        branch="master",
        file_filter=lambda file_path: file_path.endswith(".py")
    )
    ndata = loader.load()
    print(len(ndata))


def loader_with_picture():
    # pip install pdfminer
    loader = UnstructuredImageLoader("files/computer.png")
    print(loader.load())


def loader_with_image_captions():
    list_image_urls = [
        "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Hyla_japonica_sep01.jpg/260px-Hyla_japonica_sep01.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Tibur%C3%B3n_azul_%28Prionace_glauca%29%2C_canal_Fayal-Pico%2C_islas_Azores%2C_Portugal%2C_2020-07-27%2C_DD_14.jpg/270px-Tibur%C3%B3n_azul_%28Prionace_glauca%29%2C_canal_Fayal-Pico%2C_islas_Azores%2C_Portugal%2C_2020-07-27%2C_DD_14.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Thure_de_Thulstrup_-_Battle_of_Shiloh.jpg/251px-Thure_de_Thulstrup_-_Battle_of_Shiloh.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Passion_fruits_-_whole_and_halved.jpg/270px-Passion_fruits_-_whole_and_halved.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Messier83_-_Heic1403a.jpg/277px-Messier83_-_Heic1403a.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/2022-01-22_Men%27s_World_Cup_at_2021-22_St._Moritz%E2%80%93Celerina_Luge_World_Cup_and_European_Championships_by_Sandro_Halank%E2%80%93257.jpg/288px-2022-01-22_Men%27s_World_Cup_at_2021-22_St._Moritz%E2%80%93Celerina_Luge_World_Cup_and_European_Championships_by_Sandro_Halank%E2%80%93257.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Wiesen_Pippau_%28Crepis_biennis%29-20220624-RM-123950.jpg/224px-Wiesen_Pippau_%28Crepis_biennis%29-20220624-RM-123950.jpg",
    ]
    loader = ImageCaptionLoader(images=list_image_urls)
    index = VectorstoreIndexCreator().from_loaders([loader])
    query = "What's the painting about?"
    index.query(query)
    query = "What kind of images are there?"
    index.query(query)


def loader_with_xml():
    loader = UnstructuredXMLLoader(
        "files/factbook.xml",
    )
    docs = loader.load()
    print(docs[0])


if __name__ == '__main__':
    loader_with_xml()